package fr.iut2.tc1.premierprogramme.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import fr.iut2.tc1.premierprogramme.client.MyServiceGallerie;
import fr.iut2.tc1.premierprogramme.client.MyServiceGallerieAsync;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class PremierProgramme implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	private static PremierProgramme SINGLETON;
	private PhotoShow composite1;
	private Galerie composite2;
	private final MyServiceGallerieAsync MyServiceGallerie = GWT
			.create(MyServiceGallerie.class);
	private DockPanel dockpanel;
	
	AsyncCallback<ArrayList<String>> photosCallback = new AsyncCallback<ArrayList<String>>() {

		@Override
		public void onFailure(Throwable caught) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onSuccess(ArrayList<String> listePhotos) {
			// TODO Auto-generated method stub
			for(String photo : listePhotos){
				System.out.println(photo);
			}
			composite2 = new Galerie(listePhotos);
			composite1 = new PhotoShow(listePhotos.get(0));
			dockpanel.add(composite1, dockpanel.CENTER);
			dockpanel.add(composite2, dockpanel.SOUTH);
			RootPanel.get().add(dockpanel);
		}
		
	};
	
	public void onModuleLoad() {
		SINGLETON = this;
		Label Titre = new Label("Galerie Photos");
		Titre.getElement().setId("pc-template-titre");
		dockpanel = new DockPanel();
		dockpanel.setSize("400px", "400px");
		dockpanel.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
		dockpanel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
		dockpanel.setWidth("100%");
		dockpanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		dockpanel.getElement().setId("pc-template-dockPanel");
		dockpanel.add(Titre, dockpanel.NORTH);
		MyServiceGallerie.getPhotos(photosCallback);
		

		AsyncCallback<String> autreCallback = new AsyncCallback<String>() {
			public void onFailure(Throwable ex) {
				RootPanel.get().add(new HTML("oups : " + ex.toString()));
			}

			@Override
			public void onSuccess(String result) {
				// TODO Auto-generated method stub
				System.out.println(result);
			}
		};
		// Effectuer l'appel au service
		MyServiceGallerie.autreMethode(autreCallback);
	}

	public static PremierProgramme get() {
		return SINGLETON;
	}

	public PhotoShow getPhotoShow() {
		return composite1;
	}

	public Galerie getGalerie() {
		return composite2;
	}
}
